/**
 * @file   XMLUtils.java
 * @date   2012-11-8下午11:53:07
 * @author User
 */
package com.gzceb.zhgjj.util;

import java.io.IOException;
import java.io.StringReader;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;





/**
 * <p>XML工具类。</p>
 *
 * @date   2012-11-8下午11:53:07 
 * @author liqing
 * @description 
 */
public class XMLUtils  {
	private static final Log logger = LogFactory.getLog(XMLUtils.class);
	
	public static Map<String,String>  XMLParser(String xmlDoc){
		Map<String, String> resultMap = new HashMap<String, String>();
		
		//创建一个新的字符串
        StringReader read = new StringReader(xmlDoc);
        //创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
        InputSource source = new InputSource(read);
        //创建一个新的SAXBuilder
        SAXBuilder sb = new SAXBuilder();
        try{
        	//通过输入源构造一个Document
            Document doc = sb.build(source);
            //取的根元素
            Element root = doc.getRootElement();
            
            //得到根元素所有子元素的集合
            //List jiedian = root.getChildren();
            getBottom(root, resultMap);
  
        }catch (JDOMException je){
        	logger.debug("find the exception: " + "\n" + je.getLocalizedMessage());
        	je.printStackTrace();
        }catch (IOException e){
        	logger.debug("find the exception: " + "\n" + e.getLocalizedMessage());
        	e.printStackTrace();
        }finally{
        	read.close();
        }
        return resultMap;
	}
	
	public static void getBottom(Element root, Map<String, String>result){
		List jiedian = root.getChildren();
		Element et = null;
		for(int i = 0; i < jiedian.size(); i++){
			et =(Element) jiedian.get(i);
			if( 0 != et.getChildren().size()){
				getBottom(et , result);
			}
			else{
				result.put(et.getName().trim(), et.getValue().trim());
			}
		}
	}
	
	public static void main(String args[]){
		/*
		String xmlDoc = "<message>"
				+"	<head>"
				+"		<Tx_code>TTF001</Tx_code>"
				+"		<BankId>0591     </BankId>"
				+"		<BranId>520</BranId>"
				+"		<TxSerial>                     </TxSerial>"
				+"		<Teller>贷款测试1   </Teller>"
				+"		<Checker>贷款测试1   </Checker>"
				+"		<Auth>贷款测试1   </Auth>"
				+"		<Agent_no>99     </Agent_no>"
				+"		<TxDate>20120327  </TxDate>"
				+"		<TxTime>223329    </TxTime>"
				+"		<TallyDate>20120210</TallyDate>"
				+"		<TxOrgNo>       </TxOrgNo>"
				+"		<TxChannel> </TxChannel>"
				+"	</head>"
				+"	<body>"
				+"		<File_Type>8</File_Type>"
				+"		<File_Name>LC990591201203270001</File_Name>"
				+"	</body>"
				+"</message>";
		*/
		String xmlDoc = "<root>"
				+"	<jydm>PUTFILE</jydm>"
				+"	<jyrq>20120215</jyrq>"
				+"	<wjlx>1</wjlx>"
				+"	<wjm>/dqzxt/file/jjzx/DJ010500201202150001</wjm>"
				+"</root>";
		XMLUtils xmlUtils = new XMLUtils();
		Map<String, String> result = xmlUtils.XMLParser(xmlDoc);
		String str = result.get("TxOrgNo");
	}

}
